function S_fsa
%function S_fsa
%Calls FSA
%
%This function is part of SpectrHA utility
%MMA, Jul-2003
%martinho@fis.ua.pt


global FSTA FLOAD HANDLES LOOK DATA ETC

S_pointer

%---------------------------------------------------------------------
% get vector to analyse:
%---------------------------------------------------------------------
if ~S_isserie
  errordlg('FFT can be only used with series, not ellipses...','wrong...','modal');
  return
end

S_pointer('watch');
[x,field,level,interval]=S_set_data;
if isempty(x)
  S_pointer
  return
end

%---------------------------------------------------------------------
% FFT:
%---------------------------------------------------------------------
s=fsa(x,interval,0);
N=s.N;

% choose main components:
[Max, Min]=max_min(s.amp,0);
[I,J]=sort(Max.val);
Mamp=Max.val(J);
MT=s.T(Max.i(J));
Mpha=s.pha(Max.i(J));

% to use in plotit:
tide_con=flipud([Mamp Mpha]);
tide_con=tide_con(1:min(10,size(tide_con,1)),:);
tide_freq=flipud(1./MT);
tide_freq=tide_freq(1:min(10,length(tide_freq)));

%---------------------------------------------------------------------
% output
%---------------------------------------------------------------------
%head:
if S_isstation
  fname=FSTA.name;
  s4=sprintf(' Station:%4d x %4d',FSTA.i);
else
  fname=FLOAD.name;
  s4=sprintf(' Position: %3.3f x %3.3f',FLOAD.position(1), FLOAD.position(2));
end
s1=sprintf('   Fourier Spectral Analysis');
s2=sprintf(' Date: %s',datestr(now));
s3=sprintf(' File: %s',fname);
s4=s4;
s5=sprintf(' Serie Length: %d',N);
s6=sprintf(' Interval: %2.2f h',interval);
s7=sprintf(' Days: %2.2f',N*interval/24);
s8=sprintf(' Field: %s',field);
s9=sprintf(' Level: %s',level);
s10=sprintf(' a0: %4.4f',s.a0);
s11=sprintf(' Main signs');
s12=sprintf('-------------------------------------------');
s13=sprintf('       Period    Amp      Pha');
s_head=strvcat(s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13);
%values:
s_val='';
for j=1:min(length(Max.i),10)
  sc=sprintf('%13.4f %8.4f %9.4f', MT(end-j+1), Mamp(end-j+1), Mpha(end-j+1) );
  s_val=strvcat(s_val,sc);
end
s_out=strvcat(s13,s_val);
set(HANDLES.output,'string',s_out);
a=sprintf('=====================================================');

% log:
s_out=strvcat(a,s_head,s_val,a);
fid = fopen(ETC.logname,'a');
for i=1:size(s_out,1)
  fprintf(fid,'%s\n',s_out(i,:));
end
fclose(fid);

% txt_head:
if S_isstation
  txt=sprintf('FSA: Station%4d x %4d . %4s :%s %s',FSTA.i,field,level,FSTA.name);
else
  txt=FLOAD.name;
end
set(HANDLES.txt_head,'string',txt);
ETC.txt_head=txt;

%---------------------------------------------------------------------
% plot:
%---------------------------------------------------------------------
axes(HANDLES.spectrum_axes);
xlim=get(gca,'xlim');

%enable xlim buttons:
S_enable_xlim('on');

% check if necessary to hold off...
if S_isplotted('series','ellipse')
  hold off
  set(HANDLES.hold_spect,'value',0);
end

% plot:
ETC.handles.plot=plot(s.T,s.amp);
set(ETC.handles.plot,'color',LOOK.color.plot);

% set plotted variable:
ETC.plotted='fsa';

if ishold
  handle=auto_color(HANDLES.spectrum_axes,ETC.handles.plot);
  ETC.handles.plot=[];
end

% limits:  
val=ETC.default_xlim;
axis auto
if ishold % keep xlim if hold is on !
  set(gca,'xlim',xlim);
else
  set(gca,'xlim',[val(1) val(2)]);
end

xlim=get(gca,'xlim');
set(HANDLES.xlim_i,'string',xlim(1));
set(HANDLES.xlim_e,'string',xlim(2)); 

S_axes_prop

%---------------------------------------------------------------------
% plotit:
%---------------------------------------------------------------------
if ishandle(ETC.plotit)
  delete(ETC.plotit);
end % cos of the legend...
set(HANDLES.output,'value',1); % select the first line of output

%---------------------------------------------------------------------
% legend:
%---------------------------------------------------------------------
if S_isstation
  currentLegend=sprintf('FSA sta: %4d x %4d:%4s:%s',FSTA.i,field,level);
else
  currentLegend=sprintf('%0.8s %s','FSA  ',FLOAD.name);
end

if ishold
  ETC.legend=strvcat(ETC.legend,currentLegend);
else
  ETC.legend=currentLegend;
end
S_upd_legend(ETC.legend);

%---------------------------------------------------------------------
%final settings...
S_pointer

% tidestruc is used by t_predic and generated by LSF or T_TIDE
% in this case is only used by plotit!
ETC.tidestruc.tidecon=tide_con;
ETC.tidestruc.freq=tide_freq;
ETC.tidestruc_origin='fsa'; 
return

